<template>
  <div>
    <van-swipe class="my-swipe" indicator-color="darked">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" width="100%" style="display:block;" />
      </van-swipe-item>
    </van-swipe>
    <tips></tips>
    <div class="info">
      <h3>{{ info.name }}</h3>
      <p>{{ info.goods_brief }}</p>
      <div>{{ info.retail_price | filterMoney }}</div>
    </div>
    <van-cell class="showSku" title="展示弹出层" is-link @click="openPopup" />
    <div class="attribute">
      <h4>商品参数</h4>
      <ul>
        <li v-for="(item, index) in attribute" :key="item.index">
          <span>{{ item.name }}</span>
          <section class="van-ellipsis">{{ item.value }}</section>
        </li>
      </ul>
    </div>
    <!--html标签渲染到标签中
    ref 操作dom
    -->
    <div class="box" ref="box"></div>
    <div class="title">
      <span>常见问题</span>
    </div>
    <ul class="issue">
      <li v-for="item in issue" :key="item.id">
        <h3>{{ item.question }}</h3>
        <p>{{ item.answer }}</p>
      </li>
    </ul>
    <div class="title">
      <span>大家都在看</span>
    </div>
    <product :goodsList="goodsList" />
    <van-sku
      v-model="show"
      :sku="sku"
      :goods="goods"
      :hide-stock="sku.hide_stock"
      ref="sku"
    />
    <MyGoodsAction @addToCart="addToCart" :badge="badge"></MyGoodsAction>
  </div>
</template>

<script>
import { GetGoodsNum } from "../request/api";
import MyGoodsAction from "../components/MyGoodsAction";
import product from "../components/product";
import { GetGoodsDetail, GetAboutShop, GetAddCart } from "../request/api";
import tips from "../components/tips";
export default {
  data() {
    return {
      // 轮播图图片
      gallery: [],
      // 商品信息
      info: {},
      // 商品参数
      attribute: [],
      // 常见问题
      issue: [],
      // 相关产品
      goodsList: [],
      productList: [],
      // sku的显示状态
      show: false,
      goods: {
        picture: ""
      },
      sku: {
        tree: [],
        hide_stock: false,
        price: "",
        stock_num: ""
      },
      // 购物车商品数量
      badge: 0
    };
  },
  components: {
    tips,
    product,
    MyGoodsAction
  },
  created() {
    // 是一个对象不能直接写
    GetGoodsDetail({ id: this.$route.query.id }).then(res => {
      if (res.errno == 0) {
        console.log(res.data);
        // 解构赋值
        let { gallery, info, attribute, issue, productList } = res.data;
        this.gallery = gallery;
        this.info = info;
        this.attribute = attribute;
        // 问题
        this.issue = issue;

        this.productList = productList;
        // 将图片渲染到box中(v-html)
        this.$refs.box.innerHTML = info.goods_desc;
        // 渲染数据sku
        this.goods.picture = info.list_pic_url;
        // 价格
        this.sku.price = info.retail_price.toFixed(2);
        // 库存
        this.sku.stock_num = info.goods_number;
      }
    });
    // 获取相关产品
    GetAboutShop({
      id: this.$route.query.id
    }).then(res => {
      if (res.errno == 0) {
        // console.log(res.data);
        this.goodsList = res.data.goodsList;
      }
    });
    // 获取商品数量
    GetGoodsNum().then(res => {
      if (res.errno == 0) {
        console.log(res.data);
        this.badge = res.data.goodsCount;
      }
    });
  },
  methods: {
    openPopup() {
      this.show = !this.show;
    },
    // 点击打开关闭购物车 父级判断
    addToCart() {
      // 判断sku是否处于显示状态
      if (this.show) {
        // 正处于显示状态，直接加入购物车
        // 获取购物车里的商品数量
        // console.log(this.$refs.sku.getSkuData().selectedNum)
        let token = localStorage.getItem('token');
        if(token){
          let number = this.$refs.sku.getSkuData().selectedNum;
        GetAddCart({
          goodsId: this.$route.query.id, //地址栏的id
          productId: this.productList[0].id,
          number: number
        }).then(res => {
          if (res.errno == 0) {
            console.log(res.data.cartTotal.goodsCount);
            this.badge = res.data.cartTotal.goodsCount;
            this.show = false;
            this.$router.push("/cart");
          }
        });
        }else{
          this.$toast('请先登录')
          this.$router.push('/user')
        }
      } else {
        this.show = true;
      }
    }
  }
};
</script>
<style lang="less" scoped>
.info {
  background: #fff;
  text-align: center;
  padding: 20px 0;
  h3 {
    font-weight: normal;
    font-size: 20px;
    line-height: 50px;
  }
  p {
    color: #999;
    font-size: 10px;
    line-height: 30px;
  }
  div {
    color: darkred;
    font-size: 10px;
    line-height: 30px;
  }
}
.showSku {
  margin-bottom: 20px;
  border-top: 1px solid #ccc;
}
.attribute {
  background: #fff;
  padding: 20px 4%;
  h4 {
    font-weight: normal;
    font-size: 16px;
    margin-bottom: 20px;
  }
  ul {
    li {
      background: #efefef;
      height: 40px;
      line-height: 40px;
      margin-bottom: 10px;
      border-radius: 4px;
      display: flex;
      justify-content: space-between;
      span {
        width: 20%;
        text-align: right;
      }
      section {
        flex: 1;
        padding-left: 20px;
      }
    }
  }
}
// 深度修改样式
/deep/.box {
  img {
    width: 100%;
    display: block;
  }
}
.title {
  width: 100%;
  background: #fff;
  height: 50px;
  position: relative;
  // before 和 after的区别 before可以少用一个z-index
  &::after {
    content: "";
    width: 50%;
    height: 2px;
    background: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -1px;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    background: #fff;
    display: block;
    text-align: center;
    margin: 0 auto;
    height: 50px;
    line-height: 50px;
    position: relative;
    z-index: 9;
  }
}
.issue {
  padding: 0 4%;
  background: #fff;
  li {
    h3 {
      height: 30px;
      line-height: 30px;
      padding-left: 20px;
      position: relative;
      &::before {
        content: "";
        width: 4px;
        height: 4px;
        background: darkred;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 50%;
        margin-top: -2px;
      }
    }
    p {
      font-size: 12px;
      line-height: 20px;
      padding-left: 20px;
    }
  }
}
// 让自己写的购物车模块显示  增加层级
.van-goods-action {
  z-index: 2099;
}
.van-sku-container {
  min-height: auto;
}
</style>
